//! 自定义生成器实现

use serde_json::Value;
use super::Generator;

/// 自定义生成器示例 - 中文地址生成器
pub struct ChineseAddressGenerator;

impl Generator for ChineseAddressGenerator {
    fn generate(&self) -> Value {
        use crate::regions::china::{province, rand_chinese};
        use rand::Rng;
        
        let mut rng = rand::thread_rng();
        let province_val = province();
        let city = format!("{}市", rand_chinese(2..4));
        let district = format!("{}区", rand_chinese(2..3));
        let street = format!("{}街{}号", rand_chinese(2..4), rng.gen_range(1..999));
        
        Value::String(format!(
            "{}{}{}{}",
            province_val.as_str().unwrap_or("北京市"),
            city,
            district,
            street
        ))
    }
}

/// 自定义生成器示例 - 产品信息生成器
pub struct ProductGenerator;

impl Generator for ProductGenerator {
    fn generate(&self) -> Value {
        use rand::seq::SliceRandom;
        use rand::Rng;
        
        let categories = ["电子产品", "服装", "食品", "图书", "家居"];
        let adjectives = ["高品质", "经典", "时尚", "实用", "创新"];
        let nouns = ["产品", "商品", "物品", "用品"];
        
        let mut rng = rand::thread_rng();
        let category = categories.choose(&mut rng).unwrap();
        let adjective = adjectives.choose(&mut rng).unwrap();
        let noun = nouns.choose(&mut rng).unwrap();
        let id = rng.gen_range(10000..99999);
        
        serde_json::json!({
            "id": id,
            "name": format!("{}{}{}", adjective, category, noun),
            "category": category,
            "price": rng.gen_range(10.0..1000.0),
            "in_stock": rng.gen_bool(0.8)
        })
    }
}

/// 自定义生成器工厂
pub struct CustomGeneratorFactory;

impl CustomGeneratorFactory {
    /// 创建中文地址生成器
    pub fn chinese_address() -> Box<dyn Generator> {
        Box::new(ChineseAddressGenerator)
    }
    
    /// 创建产品生成器
    pub fn product() -> Box<dyn Generator> {
        Box::new(ProductGenerator)
    }
    
    /// 创建范围数字生成器
    pub fn range_number(min: i32, max: i32) -> Box<dyn Generator> {
        struct RangeNumberGenerator {
            min: i32,
            max: i32,
        }
        
        impl Generator for RangeNumberGenerator {
            fn generate(&self) -> Value {
                use rand::Rng;
                let mut rng = rand::thread_rng();
                Value::Number(rng.gen_range(self.min..=self.max).into())
            }
        }
        
        Box::new(RangeNumberGenerator { min, max })
    }
    
    /// 创建选择生成器
    pub fn choice<T: Clone + Into<Value> + Send + Sync + 'static>(options: Vec<T>) -> Box<dyn Generator> {
        struct ChoiceGenerator<T> {
            options: Vec<T>,
        }
        
        impl<T: Clone + Into<Value> + Send + Sync> Generator for ChoiceGenerator<T> {
            fn generate(&self) -> Value {
                use rand::seq::SliceRandom;
                let mut rng = rand::thread_rng();
                self.options.choose(&mut rng)
                    .map(|item| item.clone().into())
                    .unwrap_or(Value::Null)
            }
        }
        
        Box::new(ChoiceGenerator { options })
    }
}